<!--
    页面：导入导出
    路由：/importSheet
    作者：尹竹群
    部门：查账开发部
    时间：2021-09
    编辑：
-->
<template>
  <div class="container">
    <Row v-tc-height="'100%'">
      <!-- luckysheet表格区域 -->
      <i-col span="21" v-tc-height="'100%'">
        <div class="luckysheet" id="importSheet"></div>
      </i-col>
      <!-- 控制台 -->
      <i-col span="3" v-tc-height="'100%'">
        <Card :bordered="false">
          <p slot="title">控制台</p>
          <p>
            <Row>
              <Col span="24">
                <Upload action="" :before-upload="handleUpload">
                  <Button type="primary">导入Excel</Button>
                </Upload>
              </Col>
            </Row>
            <Row v-tc-bottom="10">
              <Col span="24">
                <Button type="primary" @click="handleExport">导出Excel</Button>
              </Col>
            </Row>
            <Row v-tc-bottom="10">
              <Col span="24">
                <Button type="primary" @click="handleClear">重置表格</Button>
              </Col>
            </Row>
            <Row v-tc-bottom="10">
              <Col span="24">
                <Button type="primary" @click="handleJSON">导入JSON</Button>
              </Col>
            </Row>
            <Row v-tc-bottom="10">
              <Col span="24">
                <Button type="primary" @click="handleOutJSON">导出JSON</Button>
              </Col>
            </Row>
            <Row>
              <Col
                >请先导入文件，之后重置表格，再导入JSON，此时会模拟文件导入，导入上一次保存的Json数据</Col
              >
            </Row>
          </p>
        </Card>
      </i-col>
    </Row>
  </div>
</template>

<script>
// 引入loadsh
import { cloneDeep } from "lodash";
// 解析文件插件
import LuckyExcel from "luckyexcel";
import LuckyExport from "./utils/export";
import { exportExcel } from "./utils/exportJs";

export default {
  name: "importSheet",
  data() {
    return {
      // 表格初始化数据
      sheetData: {
        // 设定DOM容器的id
        container: "importSheet",
        // 是否显示顶部信息栏
        showinfobar: false,
        // 设定表格语言
        lang: "zh",

        // 表格
        data: [
          //数据
          {
            name: "表一", //工作表名称
            color: "",
            index: 0, //唯一id
            status: 1,
            order: 0,
            row: 20, //行数
            column: 20, //列数
            celldata: [],
            config: {},
            allowEdit: false
          },
          {
            name: "表二",
            color: "",
            index: 1,

            status: 0,
            order: 1,
            celldata: [],
            config: {}
          }
        ]
      },
      // 备份数据
      copySheetData: null,
      // 表格数据
      showSheetData: null
    };
  },
  methods: {
    /**
     * 初始化报表
     */
    initSheet() {
      window.luckysheet.create(this.showSheetData);
    },
    /**
     * 上传拦截
     * @param {File} file 选择的文件
     */
    handleUpload(file) {
      if (!file) return;

      let suffixArr = file.name.split("."),
        suffix = suffixArr[suffixArr.length - 1];

      if (suffix !== "xlsx") {
        alert("目前只支持xlsx格式的文件");
        return;
      }

      // 加载
      LuckyExcel.transformExcelToLucky(file, (exportJson, luckysheetfile) => {
        if (exportJson.sheets == null || exportJson.sheets.length == 0) {
          alert(
            "Failed to read the content of the excel file, currently does not support xls files!"
          );
          return;
        }
        window.luckysheet.destroy();

        this.showSheetData.data = exportJson.sheets;
        this.showSheetData.title = exportJson.info.name;

        this.copySheetData = cloneDeep(this.showSheetData);

        this.initSheet();
      });
      return false;
    },
    /**
     * 导出
     */
    handleExport() {
      exportExcel(window.luckysheet.getAllSheets(), "下载");
      //   const sheetData = window.luckysheet.getluckysheetfile();
      //   console.log(sheetData);
      //   LuckyExport(sheetData);
    },
    /**
     * 重置
     */
    handleClear() {
      //   console.log(this.showSheetData);
      window.luckysheet.destroy();

      this.showSheetData = cloneDeep(this.sheetData);
      this.initSheet();
    },
    /**
     * 导入JSON
     */
    handleJSON() {
      window.luckysheet.destroy();

      this.showSheetData = cloneDeep(this.copySheetData);

      this.initSheet();
    },
    /**
     * 导出
     */
    handleOutJSON() {
      console.log(this.showSheetData);
    }
  },
  mounted() {
    this.showSheetData = cloneDeep(this.sheetData);

    // 初始化报表
    this.initSheet();
  }
};
</script>

<style scoped>
.luckysheet {
  width: 100%;
  height: 100%;
}
</style>
